package tp.pr4.instructions;

import tp.pr4.NavigationModule;
import tp.pr4.RobotEngine;
import tp.pr4.Rotation;
import tp.pr4.instructions.exceptions.*;
import tp.pr4.items.ItemContainer;

public class TurnInstruction extends UndoAble{
	private Rotation rotacion;
	private NavigationModule nvm;
	private RobotEngine rbt;
	/*
	 * Last instruction
	 */
	private Rotation lastRotation;
	public TurnInstruction(Rotation rot) {
		this.rotacion = rot;
	}

	public TurnInstruction() {
		this.rotacion = Rotation.UNKNOWN;
	}

	@Override
	public Instruction parse(String cad) throws WrongInstructionFormatException {
		String[] lineArr = cad.trim().replaceAll(" +", " ").split(" ");	//parte la cadena introducida en palabras

		
			String accion = "";
			String rotacion = "";
			if(lineArr.length>=1) accion = lineArr[0]; //si existe alguna palabra, la primera será la acción
			if(lineArr.length==2) rotacion = lineArr[1]; //si hay más de una palabra, la segunda será la rotación
			Instruction ins = null;
			if(accion.equalsIgnoreCase("TURN") || accion.equalsIgnoreCase("GIRAR")){//si la acción introducida es TURN				
				ins = new TurnInstruction();
				if(rotacion.equalsIgnoreCase(Rotation.LEFT.name())){//si la rotación introducida es LEFT
					ins = new TurnInstruction(Rotation.LEFT);					
				}else if(rotacion.equalsIgnoreCase(Rotation.RIGHT.name())){//si la rotación introducida es RIGHT
					ins = new TurnInstruction(Rotation.RIGHT);					
				}else{
					throw new WrongInstructionFormatException();
				}
			}else{				
				throw new WrongInstructionFormatException();
			}
			return ins;		
	}

	@Override
	public String getHelp() {
		return "     TURN|GIRAR <LEFT | RIGHT>";
	}

	@Override
	public void configureContext(RobotEngine engine,
			NavigationModule navigation, ItemContainer robotContainer) {
		this.nvm = navigation;
		this.rbt = engine;
		
	}

	@Override
	public void execute() throws InstructionExecutionException {
		if (this.rotacion.equals(Rotation.LEFT)){
			this.nvm.initHeading(this.nvm.getCurrentHeading().getLeft());
			this.lastRotation = Rotation.LEFT;
		}else if(this.rotacion.equals(Rotation.RIGHT)){
			this.lastRotation = Rotation.RIGHT;
			this.nvm.initHeading(this.nvm.getCurrentHeading().getRight());
		}else{
			throw new InstructionExecutionException();
		}
		this.rbt.setFuel(this.rbt.getFuel() -5);
		System.out.println(this.nvm.showDirection());
		System.out.println(this.rbt.getStatus());
		
		
	}
	@Override
	public boolean isUndoAble() {

		return true;
	}
	/**
	 * revets the las instruction
	 * @throws InstructionExecutionException 
	 */
	public void undo() throws InstructionExecutionException {
		if (this.lastRotation.equals(Rotation.LEFT)){
			this.nvm.initHeading(this.nvm.getCurrentHeading().getRight());			
		}else if(this.rotacion.equals(Rotation.RIGHT)){			
			this.nvm.initHeading(this.nvm.getCurrentHeading().getLeft());
		}else{
			throw new InstructionExecutionException();
		}
		this.rbt.setFuel(this.rbt.getFuel() +5);

	}
}
